package com.ui.basic.service.admin;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.jsecurity.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.common.basic.util.ComboBoxPopulator;
import com.common.basic.util.Globals;
import com.ui.basic.command.CommandLoc;
import com.ui.basic.dao.assembler.LocAsemmbler;
import com.ui.basic.ibatis.dao.masterdata.TblMCountryDAO;
import com.ui.basic.ibatis.dao.system.TblSLocDAO;
import com.ui.basic.ibatis.model.masterdata.TblMCountryExample;
import com.ui.basic.ibatis.model.system.TblSLoc;
import com.ui.basic.ibatis.model.system.TblSLocExample;

@Service("locationService")
public class LocationServiceImpl implements LocationService {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory
			.getLog(LocationServiceImpl.class);

	@Autowired
	private TblMCountryDAO tblMCountryDAO;
	@Autowired
	private TblSLocDAO tblSLocDAO;

	public void setTblMCountryDAO(TblMCountryDAO tblMCountryDAO) {
		this.tblMCountryDAO = tblMCountryDAO;
	}

	public void setTblSLocDAO(TblSLocDAO tblSLocDAO) {
		this.tblSLocDAO = tblSLocDAO;
	}

	@Transactional(readOnly = true)
	public CommandLoc getLocation(CommandLoc commandLoc) throws SQLException {
		LOGGER.debug(">>>LocationServiceImpl.getLocation");
		if (commandLoc != null) {
			TblSLocExample example = new TblSLocExample();

			int rec = 0;
			try {
				rec = tblSLocDAO.countByExample(example);
			} catch (NullPointerException npe) {
				LOGGER.error(npe);
			}

			commandLoc.calculate(rec);
			example.setIntRowNum((commandLoc.getPgIndex() - 1)
					* commandLoc.getPgRows());
			example.setIntRowsPerPage(commandLoc.getPgRows());
			example.setOrderByClause("LOC_NAME");
			commandLoc.setData(tblSLocDAO.selectByExample(example));
		}
		return commandLoc;
	}

	@Transactional(readOnly = true)
	public void setComboData(CommandLoc commandLoc) throws SQLException {
		LOGGER.debug(">>>LocationServiceImpl.setComboData");

		TblMCountryExample countryExample = new TblMCountryExample();
		countryExample.setOrderByClause("NAME");
		commandLoc.setCountryCB(ComboBoxPopulator.populate(tblMCountryDAO
				.selectByExample(countryExample), "id", "name"));
	}

	public CommandLoc getLocationDetail(CommandLoc commandLoc)
			throws SQLException {
		LOGGER.debug(">>>LocationServiceImpl.getLocationDetail");

		commandLoc.setProperty(tblSLocDAO
				.selectByPrimaryKey(commandLoc.getId()));

		return commandLoc;
	}

	public void save(CommandLoc commandLoc) throws SQLException {
		LOGGER.debug(">>>LocationServiceImpl.save");

		LocAsemmbler assembler = new LocAsemmbler();
		TblSLoc location = assembler.toEntity(commandLoc);

		if (commandLoc.getId() != null) {
			location.setChangedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			location.setChangeddate(new Date());
			try {
				tblSLocDAO.updateByPrimaryKeySelective(location);

			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		} else {
			location.setCreatedby((String) (SecurityUtils.getSubject().getSession()
					.getAttribute(Globals.CURRENT_USER_NAME)));
			location.setCreateddate(new Date());
			try {
				tblSLocDAO.insert(location);
			} catch (SQLException e) {
				LOGGER.error("***SQL Code == " + e.getErrorCode());
				LOGGER.error("***error msg == " + e.getMessage());
				throw e;
			}
		}
	}

	public void delete(CommandLoc commandLoc) throws SQLException {
		LOGGER.debug(">>>LocationServiceImpl.delete");
		TblSLocExample example = new TblSLocExample();
		
		if(commandLoc.getId()!=null){
			example.createCriteria().andIdEqualTo(commandLoc.getId());
		}else if(commandLoc.getSelectedRecord()!=null && commandLoc.getSelectedRecord().length>0){
			List<Long> idList = new ArrayList<Long>();
			for(String strId: commandLoc.getSelectedRecord())
				idList.add(Long.valueOf(strId));
			example.createCriteria().andIdIn(idList);
		}
		
		tblSLocDAO.deleteByExample(example);	
	}

}
